<?php

namespace app\common\model;

use think\Db;
use think\Model;

class Coupon extends Base
{
    protected $redata;

    /**
     * 快捷方法 返回出错信息
     */
    protected function err($msg)
    {
        $this->redata['msg'] = $msg;
        $this->redata['status'] = 0;
        return $this->redata;
    }

    /**
     * 快捷方法 返回成功
     */
    protected function suc($data = '')
    {
        $this->redata['content'] = $data;
        $this->redata['status'] = 1;
        return $this->redata;
    }


    /**
     *    使用优惠券
     */
    public function useCoupon($ucId, $orderAmount)
    {
        //判断参数是否合法
        if (!$ucId) {
            $couponAmount = 0;
            return $this->suc($couponAmount);
        } else {
            if (empty($orderAmount)) {
                return $this->err('缺少订单价格！');
            }
            // 查询用户优惠券
            $uc_res = model('userCoupon')->where(['id' => $ucId])->find();

            if (!$uc_res) {
                return $this->err('用户优惠券编码错误！');
            }
            //查询优惠券使用条件
            $u_res = $this->field('id,fullAmount,reduceAmount,status')->where(['id' => $uc_res['couponId']])->find();

            if (!$u_res) {
                return $this->err('优惠券编码错误！');
            }

            // 获取当前时间戳
            $now = time();
            //把优惠券过期时间转换为时间戳
            $overTime = strtotime($uc_res['overTime']);
            //判断使用
            if ($uc_res['status'] == 2) {
                return $this->err('该优惠券已经使用过');
            }
            if ($overTime < $now) {
                return $this->err('该优惠券已经过期！');
            }
            // 判断是否满足使用优惠券的需求
            if ($orderAmount < $u_res['fullAmount']) {
                return $this->err('未达到满减需求！');
            }
            if ($u_res['status'] == 0) {
                return $this->err('该优惠券已被禁用！');
            }
            // 操作用户优惠券表
            $uc_data = [
                'id' => $ucId,
                'status' => 2
            ];
            model('userCoupon')->editData($uc_data, $uc_data['id']);
            // 返回优惠金额
            $couponAmount = $u_res['reduceAmount'];
            return $this->suc($couponAmount);

        }
    }

    /**
     * 交易完成赠送优惠券
     */
    public function getOrderCoupon($cid = 0)
    {

        //验证参数合法
        if (empty($cid)) {
            return $this->err('缺少用户主键！');
        }
        $coupon = model('coupon')->where(['couponType' => 1003])->find();

        $where = [
            'cid' => $cid,
            'couponId' => $coupon['id']
        ];

        $c_res =  model('coupon')->where(['id' => $where['couponId']])->find();
        $dateType = $c_res['dateType'];

                //判断用户领取的优惠券类型  0-优惠券类型为固定时间
                if ($dateType == 0) {
                    $dateLimit = $c_res['dateLimit'] * 86400;
                    $createTime = createTime();
                    $overTime = strtotime($createTime) + $dateLimit;
                    $data = [
                        'overTime' => date('Y-m-d H:i:s', $overTime),
                    ];
                } //判断用户领取的优惠券类型  1-优惠券类型为指定时间
                else{
                    $data = [
                        'overTime' => $c_res['endTime'],
                    ];

                }
                $uc_data = [
                    'cid' => $where['cid'],
                    'couponId' => $where['couponId'],
                    'createTime' => createTime(),
                    'overTime' => $data['overTime']
                ];

                    $res = model('user_coupon')->editData($uc_data);

                    if($res){
                        return $this->suc('领取成功！');
                    };
    }
}